package com.ljy.jymail.order.web;

import com.alipay.api.AlipayApiException;
import com.ljy.jymail.order.config.AlipayTemplate;
import com.ljy.jymail.order.service.OrderService;
import com.ljy.jymail.order.vo.PayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
public class PayWebController {

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    OrderService orderService;


    /*
    * 将支付页让浏览器展示，支付成功之后，跳到用户的订单列表页
    *
    * */
    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")//产生text/html类型的数据
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {

        PayVo payDetail = orderService.getOrderPay(orderSn);
//        payVo.setBody();//订单备注
//        payVo.setSubject();//订单标题
//        payVo.setOut_trade_no();//订单号
//        payVo.setTotal_amount();//订单金额
        //返回的是一个页面，将此页面交给浏览器渲染，就是支付界面
        String pay = alipayTemplate.pay(payDetail);
        orderService.updateOrderStatus(orderSn);

        return pay;
    }
}
